package com.happy.Animation.controllers;

import com.happy.Animation.dao.CommentRepository;
import com.happy.Animation.entities.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("comments")
public class CommentController {
    @Autowired
    CommentRepository commentRepository;

    @GetMapping
    public String displayComments(Model model){
        List<Comment> comments =commentRepository.findAll();
        model.addAttribute("comments",comments);
        return "comments/list-comments";
    }

    @GetMapping("/new")
    public String displayCommentForm(Model model){
        Comment aComment = new Comment();
        model.addAttribute("comment",aComment);
        return "comments/new-comment";
    }
    @PostMapping("/save")
    public String createComment(Model model,Comment comment){
        commentRepository.save(comment);

        return "redirect:/comments/new";
    }
}
